package com.gdcp.rv




import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView

import java.util.ArrayList

class MainActivity : AppCompatActivity(), IOnDelListener {
    private var rev_list: RecyclerView? = null
    private var main_btn: Button? = null
    private val arrayList = ArrayList<Student>()
    private var myadapter: Myadapter? = null
    private var edit_keyword: EditText? = null
    private var btn_search: Button? = null
    private val ResultStudent = ArrayList<Student>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initBundle()
        initArraylist()
        main_btnEvent()
        rev_list = findViewById(R.id.rev_list)
        val layoutManager = LinearLayoutManager(this@MainActivity)
        rev_list!!.adapter = myadapter
        rev_list!!.layoutManager = layoutManager
        btn_search!!.setOnClickListener {
            val keyword = edit_keyword!!.text.toString().trim { it <= ' ' }
            searchEvent(arrayList, keyword)
            myadapter!!.changeData(ResultStudent)
        }
    }

    private fun searchEvent(arrayList: ArrayList<Student>, keyword: String) {
        ResultStudent.clear()
        for (i in arrayList.indices) {

            if (arrayList[i].name.contains(keyword)) {
                ResultStudent.add(arrayList[i])
            }


        }


    }


    private fun main_btnEvent() {

        main_btn!!.setOnClickListener {
            val intent = Intent(this@MainActivity, Add_Student::class.java)
            startActivityForResult(intent, 2000)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == 2008) {
            val name = data!!.getStringExtra("NAME")
            val age = data.getIntExtra("AGE", 0)
            val imageid = data.getIntExtra("ID", R.drawable.dog3)
            val s = Student(imageid, name, age, "00004")
            arrayList.add(s)
            myadapter!!.notifyDataSetChanged()
        }
        if (resultCode == 1000) {
            val name = data!!.getStringExtra("NAME")
            val age = data.getIntExtra("AGE", 0)
            val no = data.getStringExtra("NO")
            val imgId = data.getIntExtra("ID", R.drawable.dog1)
            for (i in arrayList.indices) {
                val s = arrayList[i]
                if (s.no == no) {
                    s.age = age
                    s.name = name
                    s.image = imgId
                    myadapter!!.notifyDataSetChanged()
                }
            }
        }
    }

    //    绑定控件；
    private fun initBundle() {
        rev_list = findViewById(R.id.rev_list)
        main_btn = findViewById(R.id.main_btn)
        btn_search = findViewById(R.id.btn_search)
        edit_keyword = findViewById(R.id.edit_search)
        myadapter = Myadapter(arrayList, this@MainActivity, this@MainActivity)
    }


    //添加学生到arraylist；
    private fun initArraylist() {
        val s1 = Student(R.drawable.dog1, "阿成", 12, "00001")
        val s2 = Student(R.drawable.dog2, "阿莱", 13, "00002")
        val s3 = Student(R.drawable.dog3, "阿云", 14, "00003")
        val s4 = Student(R.drawable.dog4, "阿大", 15, "00004")
        val s5 = Student(R.drawable.dog5, "阿才", 16, "00005")
        arrayList.add(s1)
        arrayList.add(s2)
        arrayList.add(s3)
        arrayList.add(s4)
        arrayList.add(s5)
    }


    override fun del(i: Int) {
        arrayList.removeAt(i)
        myadapter!!.notifyDataSetChanged()
    }


}

